<%@ Page Language="C#" MasterPageFile="~/MasterPages/Learning.Master" AutoEventWireup="true" CodeBehind="InversionOfControl.aspx.cs" Inherits="Mindscape.BackgroundMotion.Website.Learn.InversionOfControl" Title="Code: Learn about Inversion of Control" %>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContent" runat="server">

    <h2>Inversion of Control</h2>
    
    <p>
    A common problem encountered in software development is the level of coupling which is formed within complex systems
    that slowly causes the system to become brittle and unwilling to change. This is seen by many as one of the factors
    that lead to a high maintenance cost of software.<br />
    <br />
    To overcome this problem, an approach known as Inversion of Control (IoC) has been adopted as a favored way to loosely couple
    components in a software system so that components can be better encapsulated and they are flexible and responsive to
    change. Additionally we find that by structuring our systems in a loosely connected manner they are better able to be
    unit tested and constructed in a modular fashion which leads to better productivity for teams.<br />
    <br />
    Inversion of Control describes the approach to handling a plug-in architecture. Rather than individual components being responsible
    for understanding other components which can handle a given request, their requirements will be satisfied by an external party who
    will inject the dependencies they need.
    </p>         

    <h2>Contract First</h2>    
    
    <p>
    To support a pluggable infrastructure we must avoid referring to a dependant component by its concrete class, but rather by an
    interface which describes the contract which it fulfills. For example, in BackgroundMotion we have the notion of an application
    controller class which implements the process flow for the domain. Controllers could become a dependency for anyone who might
    use them, so to avoid this we describe them first by a contract, and then implement concrete logic to meet that contract.
    </p>
    
    <div style="font-family: Consolas, Verdana; font-size: 11pt; color: black; background: white;">
    <p style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: navy; font-weight: bold;">IContributionController</span> : <span style="color: navy; font-weight: bold;">IController</span></p>
    <p style="margin: 0px;">&nbsp; {</p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: gray;">///</span><span style="color: green;"> View a </span><span style="color: gray;">&lt;see cref="Contribution"/&gt;</span></p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: navy; font-weight: bold;">Contribution</span> ViewContribution(<span style="color: blue;">int</span> id);</p>
    <p style="margin: 0px;">&nbsp;</p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: gray;">///</span><span style="color: green;"> Gets a </span><span style="color: gray;">&lt;see cref="Contribution"/&gt;</span><span style="color: green;">.</span></p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></p>
    <p style="margin: 0px;">&nbsp; &nbsp; <span style="color: navy; font-weight: bold;">Contribution</span> GetContribution(<span style="color: blue;">int</span> id);</p>
    </div>

    <p>
    Above is an example from the IContributionController interface which is implemented by the ContributionController class. Consumers always refer to
    IContributionController.<br />
    <br />
    Once we have established a set of contracts which bound components in our system we can use one of two common techniques to satisfy the
    relationships between those components through an inversion of control.
    </p>
    
    <h2>Dependency Injection</h2>
    
    <p>
    Dependency Injection is the most common software pattern for achieving Inversion of Control in software systems. To inject a dependency we can either take
    the dependency as a property to be assigned, an argument on a constructor or by a defined method on an interface. The injection itself is typically managed
    by an external provider usually referred to as an IoC container.<br />
    <br />
    You can read more about the various types of Dependency Injection in <a href="http://www.martinfowler.com/articles/injection.html">an article from Martin Fowler</a>. There are many frameworks
    which use Dependency Injection to manage coupling, we have listed a couple in the resources section below.
    </p>
    
    <h2>Service Locators</h2>
    
    <p>
    An alternate approach to achieve Inversion of Control is to use a gateway class known as the Service Locator which will find
    an appropriate class to meet a given dependency. This is the style of IoC which was used in BackgroundMotion and was prescribed and
    supported by the <a href="WebClientSoftwareFactory.aspx">Composite Web application block</a>.<br />
    <br />
    To use the ServiceLocator, we ask it to return an instance of a class which implements a requested interface, e.g. to return an instance of
    an IContributionController we would write the following code.
    </p>       
    
    <div style="font-family: Consolas, Verdana; font-size: 11pt; color: black; background: white;">
    <p style="margin: 0px;">&nbsp; <span style="color: navy; font-weight: bold;">IContributionController</span> controller = <br />
        &nbsp; &nbsp;&nbsp; &nbsp;<span style="color: navy; font-weight: bold;">ServiceLocator</span>.Get&lt;<span style="color: navy; font-weight: bold;">IContributionController</span>&gt;();</p>
    <p style="margin: 0px;">&nbsp; <span style="color: navy; font-weight: bold;">Contribution</span> contribution = controller.GetContribution(id);</p>
    </div>
    
    <p>
    Services are initialized into the container by declaring a class which inherits from Microsoft.Practices.CompositeWeb.ModuleInitializer and then implementing 
    and overridden Configure method. In BackgroundMotion our ModuleInitializer lives under the Mindscape.BackgroundMotion.Website.Infrastructure namespace.<br />
    <br />
    Services are associated with the interface which they are implementing as shown below:    
    </p>
    
    <div style="font-family: Consolas, Verdana; font-size: 11pt; color: black; background: white;">
    <p style="margin: 0px;">&nbsp; services.Add&lt;<span style="color: navy; font-weight: bold;">IUnitOfWorkFactory</span>&gt;(<span style="color: blue;">new</span> <span style="color: navy; font-weight: bold;">DLinqUnitOfWorkFactory</span>(<span style="color: navy; font-weight: bold;">Configuration</span>.ConnectionString));</p>
    <p style="margin: 0px;">&nbsp; services.Add&lt;<span style="color: navy; font-weight: bold;">IRepositoryFactory</span>&gt;(<span style="color: blue;">new</span> <span style="color: navy; font-weight: bold;">DLinqRepositoryFactory</span>());</p>
    <p style="margin: 0px;">&nbsp; services.Add&lt;<span style="color: navy; font-weight: bold;">IReferenceDataProvider</span>&gt;(<span style="color: blue;">new</span> <span style="color: navy; font-weight: bold;">ReferenceDataProvider</span>(httpContextAdapter));</p>
    <p style="margin: 0px;">&nbsp; services.Add&lt;<span style="color: navy; font-weight: bold;">IMembershipController</span>&gt;(<span style="color: blue;">new</span> <span style="color: navy; font-weight: bold;">MembershipController</span>());</p>
    <p style="margin: 0px;">&nbsp; services.Add&lt;<span style="color: navy; font-weight: bold;">IContributionController</span>&gt;(<span style="color: blue;">new</span> <span style="color: navy; font-weight: bold;">ContributionController</span>());</p>
    </div>

    <br />
    <h2>Resources</h2>

    <ul>
        <li><a href="http://www.martinfowler.com/articles/injection.html">Martin Fowler describes Inversion of Control and Dependency Injection</a></li>        
        <li>Castle Project: <a href="http://www.castleproject.org/container/index.html">MicroKernel and Windsor IOC Containers</a></li>
        <li><a href="http://www.springframework.net/index.html">Spring.NET Application Framework</a></li>
    </ul>    
    
</asp:Content>
